package com.wenx.cxfCamel;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.camel.component.ActiveMQComponent;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.RouteDefinition;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ByteArrayResource;

import javax.jms.ConnectionFactory;

public class CxfWebService1 {

    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext(); // 1. 创建 CamelContext.
        context.setTracing(true);
        context.start(); // 4. 启动CamelContext.
        context.addRouteDefinition(initRoute());
        Thread.sleep(Integer.MAX_VALUE);  // 为了保持CamelContext处于工作状态，这里需要sleep主线程
        context.stop(); // 最后停止CamelContext
    }

    public static RouteDefinition initRoute(){
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
                "<beans xmlns=\"http://www.springframework.org/schema/beans\""+
                "       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
                "       xmlns:cxf=\"http://camel.apache.org/schema/cxf\""+
                "       xsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"+
                "       http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf-2.5.0.xsd\">"+
                "   <cxf:cxfEndpoint id=\"reportIncident\" " +
                "       address=\"http://localhost:8080/camel-example-cxf-proxy/webservices/incident\" " +
                "       endpointName=\"s:ReportIncidentEndpoint\" serviceName=\"s:ReportIncidentEndpointService\""+
                "       xmlns:s=\"http://reportincident.example.camel.apache.org\"/>"+
                "</beans>";
        XmlBeanFactory factory = new XmlBeanFactory(new ByteArrayResource(xml.getBytes()));
        //acf.registerBeanDefinition("helloWorld", factory.getMergedBeanDefinition("helloWorld"));

        RouteDefinition routeDefinition = new RouteDefinition();
        routeDefinition.from("cxf:http://localhost:8080/camel-example-cxf-proxy/webservices/incident" +
                "?serviceName=ReportIncidentEndpointService" +
                "&endpointName=ReportIncidentEndpoint" +
                "&dataFormat=MESSAGE" +
                "&defaultOperationNamespace=http://reportincident.example.camel.apache.org")
                .bean(EnrichBean.class,"enrich")
                .removeHeader("CamelHttp*")
                .to("http://10.10.5.213:9081/real-webservice?throwExceptionOnFailure=false");
        return routeDefinition;
    }
}
